<?php

class BGoodsCategories extends BaseModel
{
    protected $res;
    protected $key = "admin_";

    public function getSource()
    {
        return "f_b_goods_categories";
    }

    public function initialize()
    {
        parent::initialize();
    }

    public function goods_categories_all($columns = '*')
    {
        $list = $this::find(
            array(
                "columns" => $columns,
            )
        );
        return $list;
    }

    public function goods_categories_parents()
    {
        $condition = 'parent_id = 0 ';
        $all       = $this::find(
            array(
                $condition,
                "order"   => 'id asc',
                "columns" => 'id,name',
            ));
        return $all;
    }

    public function goods_categories_list($params = [])
    {
        $page = intval($params['page']);
        $size = intval($params['limit']);
        if ($page > 0) {
            $page = ($page - 1) * $size;
        }
        $condition = '1=1 ';
        if (isset($params['keywords']) && !empty($params['keywords'])) {
            $condition .= " and title like '%{$params['keywords']}%'";
        }
        $cate_list = $this::find(
            array(
                $condition,
                "order"  => 'created_at desc',
                //                "columns" => 'id,cid,item_id,title,short_title,shop_type,pic_logo,coupon_money,price,end_price,month_sale,site,created_at,updated_at,top,status',
                'limit'  => $size,
                'offset' => $page
            ));


        return $cate_list;
    }

    public function goods_categories_count($params = [])
    {

        $condition = '1=1 ';
        if (isset($params['keywords']) && !empty($params['keywords'])) {
            $condition .= " and title like '%{$params['keywords']}%'";
        }
        $cate_list = $this::count(
            array(
                $condition,
                "columns" => 'id',
            ));

        return $cate_list;
    }

    public function find_by_id($id)
    {
        $info = $this->findFirstById($id)->toarray();
        if ($info) {
            return $info;
        } else {
            return [];
        }
    }

    public function find_by($parent_id, $value)
    {
        $info = $this::findFirst('parent_id =' . $parent_id . ' and value=' . $value);
        if ($info) {
            return $info->toarray();
        } else {
            return [];
        }
    }

    public function find_by_parent_id($id)
    {
        $info = $this::findFirst('parent_id =' . $id);
        if ($info) {
            return $info->toarray();
        } else {
            return [];
        }
    }

    public function save_categories($params)
    {
        return $this::save($params);
    }

}
